package com.cuitrip.business.tripservice;


import android.content.Intent;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;
import com.lab.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by sheena on 15/12/17.
 * 创建行程引导页面
 */
public class CreateTripGuideActivity extends CustomUiFragmentActivity {

    public static final int PAGE_SIZE = 4;
    private int[] mImageIds = new int[]{R.drawable.card_01, R.drawable.card_02, R.drawable.card_03, R.drawable.card_04};

    @Bind(R.id.vp_guide)
    ViewPager mVpGuide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.create_trip_guide_activity);
    }

    @Override
    protected boolean appTheme() {
        return false;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        MyAdapter myAdapter = new MyAdapter();
        mVpGuide.setAdapter(myAdapter);

    }


    @Override
    public boolean initCloseTopBar() {
        return true;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        return null;
    }

    public class MyAdapter extends PagerAdapter {


        public MyAdapter() {
        }

        @Override
        public int getCount() {
            return PAGE_SIZE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_guide_adapter, null);
            ImageView guidePic = (ImageView) view.findViewById(R.id.iv_guidePic);
            LinearLayout llContainer= (LinearLayout) view.findViewById(R.id.container);
            TextView guideDesc1 = (TextView) view.findViewById(R.id.tv_guideDesc1);
            TextView guideDesc2 = (TextView) view.findViewById(R.id.tv_guideDesc2);
            TextView btnJoin = (TextView) view.findViewById(R.id.btn_join);
            guidePic.setImageResource(mImageIds[position]);
            for (int i = 0; i < 4; ++i) {
                //圆点
                ImageView point = new ImageView(CreateTripGuideActivity.this);
                point.setImageResource(R.drawable.pointer_selector);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                if (i > 0) {
                    params.leftMargin = DisplayUtil.dip2px(8.f);
                    point.setEnabled(false);
                }
                point.setLayoutParams(params);
                llContainer.addView(point);
            }
            switch (position) {
                case 0:
                    setIcon(position,llContainer,4);
                    guideDesc1.setText(getString(R.string.insider_guide_title1));
                    guideDesc2.setText(getString(R.string.insider_guide_desc1));
                    btnJoin.setVisibility(View.GONE);
                    llContainer.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    setIcon(position,llContainer,4);
                    guideDesc1.setText(getString(R.string.insider_guide_title2));
                    guideDesc2.setText(getString(R.string.insider_guide_desc2));
                    btnJoin.setVisibility(View.GONE);
                    llContainer.setVisibility(View.VISIBLE);
                    break;

                case 2:
                    setIcon(position,llContainer,4);
                    guideDesc1.setText(getString(R.string.insider_guide_title3));
                    guideDesc2.setText(getString(R.string.insider_guide_desc3));
                    btnJoin.setVisibility(View.GONE);
                    llContainer.setVisibility(View.VISIBLE);
                    break;

                case 3:
                    guideDesc1.setText(getString(R.string.insider_guide_title4));
                    guideDesc2.setText(getString(R.string.insider_guide_desc4));
                    llContainer.setVisibility(View.GONE);
                    btnJoin.setVisibility(View.VISIBLE);
                    break;

            }
            btnJoin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TripEditActivity.navigate2CreateTripActivity(CreateTripGuideActivity.this,true);
                    finish();

                }
            });

            container.addView(view);
            return view;
        }

        private void setIcon(int position,LinearLayout ll,int max){
            for (int i=0;i<max;++i){
                if (i==position){
                    ll.getChildAt(i).setEnabled(true);
                }else{
                    ll.getChildAt(i).setEnabled(false);
                }
            }
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
